/**
  ******************************************************************************
  * @file    main.c
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   Main program body 
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_dbgmcu.h"
#include "global.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "task.h"


/* Includes ------------------------------------------------------------------*/
extern void create_tasks(void);

void PWR_vPVDInit(void)
{ 
    /* Configure the PVD Level to 3 and generate an interrupt on rising and falling
    edges(PVD detection level set to 2.9V, refer to the electrical characteristics
    of you device datasheet for more details) */
    PWR_PVDLevelConfig(PWR_PVDLevel_2V9);
    PWR_PVDCmd(ENABLE);
    while((PWR->CSR & PWR_CSR_PVDO)!=0);
}


/**
* @brief    This function initializes the all the I/O ports.  It is called by the main function
* @param    none
* @note     none
* @retval   none
*/
void GPIO_Initial(void)
{
//    u32 led_output;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);

    /* GPIOA, GPIOB, GPIOC, GPIOD*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

    /* Enable TIM1 TIM2 TIM3 TIM4 and TIM5 CLOCK*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5, ENABLE);

    /* CANx Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

    /* Enable PWR and BKP clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE);

    PWR_vPVDInit();
    

    /*Configure pins: PA10 key switch*/
    
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    // PA0: LCD_DB0
    // PA1: LCD_DB1
    // PA2: LCD_DB2
    // PA3: LCD_DB3
    // PA4: LCD_DB4
    // PA5: LCD_DB5
    // PA6: LCD_DB6
    // PA7: LCD_DB7
    // PA9: bell out
    // PA15: digicod_control
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |  GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | 
                                  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_9 |GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /*Configure pins: PB0 mcu_digital_in 02*/
    /*Configure pins: PB1 mcu_digital_in 03*/
    /*Configure pins: PB3 key1*/
    
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    
    /* Configure PB.6 TIM4_CH1 lcd backlight PWMOUT*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    /*Configure pins: PB7 LCD_CS1*/
    /* Configure PB.5 LCD_RST*/
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7 | GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    

    /*Configure pins: PC0 LCD_CS2*/
    /*Configure pins: PC1 LCD_DI*/
    /*Configure pins: PC2 LCD_RW*/
    /*Configure pins: PC3 LCD_E*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /*Configure pins: PC4 VIN_AD*/
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    

    /*Configure pins: PC5 mcu_digital_in 01*/
    /*Configure pins: PC12 key3*/
    /*Configure pins: PC13 key4*/
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /*Configure pins: PD2 key2*/
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

} 

void Iwatchdog_Init()
{
    /* IWDG timeout equal to 280 ms (the timeout may varies due to LSI frequency
    dispersion) */
    /* Enable write access to IWDG_PR and IWDG_RLR registers */
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

    /* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
    IWDG_SetPrescaler(IWDG_Prescaler_32);

    /* IWDG timeout = 2500/1250 = 2s */
    IWDG_SetReload(I_WATCHDOG_TIMEOUT);

    /* Reload IWDG counter */
    IWDG_ReloadCounter();

    /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
    IWDG_Enable();
}


/**
  * @brief  Configures the SysTick.
  * @param  None
  * @retval None
  */
void SysTick_Configuration(void)
{
    /* Setup SysTick Timer for 1 msec interrupts  */
    if (SysTick_Config((SystemCoreClock) / 1000))
    { 
        /* Capture error */ 
        while (1);
    }

    //NVIC_SetPriority(SysTick_IRQn, 0x0);
}

 

/**
* @brief    Main program.
* @param    none
* @note     none
* @retval   none
*/
int main(void)
{
    JumpToApp();  
    GPIO_Initial();
    SysTick_Configuration();
    create_tasks();
}

